package com.biomatch.controller;

import com.biomatch.domain.Role;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.RoleService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("lock.role")
@RequestMapping(value = "/role", method = RequestMethod.POST)
@Api(value = "角色管理")
public class RoleController {

    @Autowired
    private Internationalize internationalize;

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/getAllRoles")
    @ApiOperation(value = "获得全部角色", response = Role.class, httpMethod = "POST")
    public RestResult<List<Role>> getAllRoles() throws Exception {
        return RestResponse.success(roleService.getAllRoles());
    }

    @RequestMapping(value = "/getRolesByPage")
    @ApiOperation(value = "获得角色分页列表", response = Role.class, httpMethod = "POST")
    public RestResult<Role> getRoleByPage(@RequestBody Pagination pagination) throws Exception {
        return RestResponse.success(roleService.getRolesByPage(pagination));
    }

    @RequestMapping(value = "/saveOrUpdateRole")
    @ApiOperation(value = "新增或更新角色", response = Role.class, httpMethod = "POST")
    public RestResult<Role> saveOrUpdateRole(@RequestBody Role role) throws Exception {
        Role result = roleService.saveOrUpdateRole(role);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("role.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value = "/deleteRole")
    @ApiOperation(value = "删除角色", response = Role.class, httpMethod = "POST")
    public RestResult<Role> deleteRole(@RequestBody Role role) throws Exception {
        Role result = roleService.deleteRole(role);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("role.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }
}
